

import 'package:elevator/get/intl_keys.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';

import 'login_logic.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({Key? key}) : super(key: key);

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  var userNameController = TextEditingController();
  var passWordController = TextEditingController();
  final logic = Get.put(LoginLogic());
  final state = Get.find<LoginLogic>().state;

  @override
  void initState() {
    super.initState();
    if (!kReleaseMode) {
      userNameController.text = "";
      // userNameController.text = "ly";
      passWordController.text = "";
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage('images/app_bg.png'),
            fit: BoxFit.cover,
          ),
        ),
        child: Container(
            alignment: Alignment.center,
            padding: const EdgeInsets.symmetric(horizontal: 8.0),
            child: SingleChildScrollView(
              scrollDirection: Axis.vertical, // 滚动方向，默认是垂直方向
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  const Image(
                      image: AssetImage("images/logo.png"), width: 160.0),

                  const SizedBox(
                    height: 10,
                  ),

                  Text(IntlKeys.systemName.tr,
                      style: const TextStyle(
                        fontSize: 20.0, // 文字大小
                        color: Colors.white, // 文字颜色
                      )),

                  const SizedBox(
                    height: 80,
                  ),

                  ///用户名
                  buildAccountInputWidget(userNameController),

                  const SizedBox(height: 20),

                  ///密码
                  buildPasswordInputWidget(passWordController),

                  ///距离上一个View距离
                  const SizedBox(
                    height: 50,
                  ),

                  ///登陆按钮
                  buildLoginButton(context),
                ],
              ),
            )),
      ),
    );
  }

  ///手机号
  Widget buildAccountInputWidget(TextEditingController? userNameController) {
    return Container(
        alignment: Alignment.center,
        padding: const EdgeInsets.symmetric(horizontal: 8.0),
        child: TextField(
          controller: userNameController,
          decoration: InputDecoration(
            filled: true,
            fillColor: Colors.white,
            hintText: IntlKeys.username.tr,
            prefixIcon: const Icon(Icons.person),
            focusedBorder: const OutlineInputBorder(
              ///设置边框四个角的弧度
              borderRadius: BorderRadius.all(Radius.circular(10)),

              ///用来配置边框的样式
              borderSide: BorderSide(
                ///设置边框的颜色
                color: Colors.orange,

                ///设置边框的粗细
                width: 1.0,
              ),
            ),
            border: const OutlineInputBorder(
              ///设置边框四个角的弧度
              borderRadius: BorderRadius.all(Radius.circular(10)),

              ///用来配置边框的样式
              borderSide: BorderSide(
                ///设置边框的颜色
                color: Colors.white,

                ///设置边框的粗细
                width: 1.0,
              ),
            ),
          ),
          style: const TextStyle(fontSize: 16),
          keyboardType: TextInputType.text,
        ));
  }

  ///密码
  Widget buildPasswordInputWidget(TextEditingController? passWordController) {
    return Container(
        alignment: Alignment.center,
        padding: const EdgeInsets.symmetric(horizontal: 8.0),
        child: Obx(() => TextField(
              controller: passWordController,
              obscureText: !state.showPassword.value,
              decoration: InputDecoration(
                filled: true,
                fillColor: Colors.white,
                hintText: IntlKeys.password.tr,
                prefixIcon: const Icon(Icons.lock),
                focusedBorder: const OutlineInputBorder(
                  ///设置边框四个角的弧度
                  borderRadius: BorderRadius.all(Radius.circular(10)),

                  ///用来配置边框的样式
                  borderSide: BorderSide(
                    ///设置边框的颜色
                    color: Colors.orange,

                    ///设置边框的粗细
                    width: 1.0,
                  ),
                ),
                border: const OutlineInputBorder(
                  ///设置边框四个角的弧度
                  borderRadius: BorderRadius.all(Radius.circular(10)),

                  ///用来配置边框的样式
                  borderSide: BorderSide(
                    ///设置边框的颜色
                    color: Colors.white,

                    ///设置边框的粗细
                    width: 1.0,
                  ),
                ),
              ),
              style: const TextStyle(fontSize: 16),
              keyboardType: TextInputType.text,
            )));
  }

  ///登陆按钮
  Widget buildLoginButton(BuildContext context) {
    final ButtonStyle style =
        ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20));
    return Container(
      padding: const EdgeInsets.fromLTRB(20, 10, 10, 20),
      child: Row(
        children: [
          Expanded(
              child: ElevatedButton(
            style: ButtonStyle(
                shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                  RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(18.0),
                  ),
                ),
                padding: MaterialStateProperty.all(const EdgeInsets.all(10)),
                foregroundColor: MaterialStateProperty.all(Colors.white),
                backgroundColor: MaterialStateProperty.all(Colors.blue),
                textStyle:
                    MaterialStateProperty.all(const TextStyle(fontSize: 20)),
                overlayColor:
                    MaterialStateProperty.all(const Color(0xFFBDBDBD))),

            onPressed: () =>
                logic.login(context, userNameController, passWordController),
            child: Text(IntlKeys.login.tr), //选中的颜色字体颜色
          ))
        ],
      ),
    );
  }


}
